/**
 *
 * 2019年09月01日11:14:05
 * mirko
 * 递归实现菜单获取
 *
 */
let result = [];
let result_organize = [];

function hasChildren(obj) {
    if (obj.hasOwnProperty('children')) {
        return true
    } else {
        return false
    }
}

function hasAuthority(need_authority, obj) {
    for (let i = 0; i < need_authority.length; i++) {
        if (obj.hasOwnProperty('role')) {
            if (obj['role'].indexOf(need_authority[i]) > -1) {
                return true
            }
        }
    }
    return false
}

const font_all_menu = [
    // { path: '/index', title: '每日简介', role: ['root'] },
    {
        title: '消息',
        path: '/message',
        children: [
            { path: '/message/messageModelList', title: '消息模版列表', role: ['root'] }
        ]
    },
    {
        title: '抖音账号',
        path: '/douyinAccount',
        children: [
            { path: '/douyinAccount/douyinAccountList', title: '抖音账号列表', role: ['root'] }
        ]
    },
    {
        title: '抖音链接',
        path: '/douyinLink',
        children: [
            { path: '/douyinLink/douyinLinkList', title: '抖音链接列表', role: ['root'] }
        ]
    },
    { path: '/adminUser/adminUserList', title: '账号管理', role: ['root'] },
    // {
    //     title: 'ex',
    //     path: '/ex',
    //     children: [
    //         { path: '/ex/ex', title: 'ex', role: ['root'] }
    //     ]
    // },
]

//递归
function split(role, obj, tm_result) {
    if (Array.isArray(obj)) {
        obj.forEach(function (value) {
            let new_array = tm_result.map(value1 => value1)
            split(role, value, new_array)
        })
    } else {
        if (hasAuthority(role, obj)) {
            let temp = {}
            temp.title = obj.title
            temp.path = obj.path
            if (obj.icon) {
                temp.icon = obj.icon
            }
            tm_result.push(temp)
            result.push(tm_result)
            return
        }
        if (hasChildren(obj)) {
            obj.children.forEach(function (value) {
                let new_array = tm_result.map(value1 => value1)
                let temp = {}
                temp.title = obj.title
                temp.path = obj.path
                if (obj.icon) {
                    temp.icon = obj.icon
                }
                new_array.push(temp)
                split(role, value, new_array)
            })
        }
    }
}

function judge(array, title) {
    let flag = false
    array.forEach(value => {
        if (value.title === title) {
            flag = true
        }
    });
    return flag
}

function organizeMenu(menu) {
    menu.forEach(value => {
        if (value.length > 0) {
            value.forEach((item, index, path) => {


                //第一层
                if (index === 0) {
                    if (!judge(result_organize, item.title)) {
                        result_organize.push(item)
                    }
                }


                //第二层
                if (index === 1) {
                    result_organize.forEach((value1, index1, path1) => {
                        if (value1.title === path[0].title) {
                            if (!result_organize[index1].children || !judge(result_organize[index1].children, item.title)) {
                                if (!result_organize[index1].children) {
                                    result_organize[index1].children = [item]
                                } else {
                                    result_organize[index1].children.push(item)
                                }
                            }
                        }
                    })
                }

                //第三层
                if (index === 2) {
                    result_organize.forEach((value1, index1, path1) => {
                        if (value1.title === path[0].title) {
                            result_organize[index1].children.forEach((value2, index2, path2) => {

                                if (value2.title === path[1].title) {
                                    if (!result_organize[index1]['children'][index2].children || !judge(result_organize[index1]['children'][index2].children, item.title)) {
                                        if (!result_organize[index1]['children'][index2].children) {
                                            result_organize[index1]['children'][index2].children = [item]
                                        } else {
                                            result_organize[index1]['children'][index2].children.push(item)
                                        }
                                    }
                                }
                            })
                        }
                    })
                }

            })
        }
    })

}


function getMenuByRole(role) {
    result = []
    result_organize = []
    if (!Array.isArray(role)) {
        role = [role]
    }
    split(role, font_all_menu, [])
    organizeMenu(result)
    return result_organize
}

//
// let finally_result = JSON.stringify(getMenuByRole(['aini']))
//
// console.log(finally_result)
module.exports = getMenuByRole